<template>
  <div class="setBirthday-container">
    <van-datetime-picker v-model="currentDate"
      type="date"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="$emit('close')"
      @confirm="onConfirm" />
  </div>
</template>

<script>
import { editUserProfile } from '@/api/user'
import dayjs from 'dayjs'

export default {
  name: 'SetBirthday',
  components: {},
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1950, 0, 1),
      maxDate: new Date(2300, 10, 1),
      currentDate: new Date(this.value)
    }
  },
  methods: {
    async onConfirm (value) {
      const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')
      console.log(value, currentDate)
      // 如果网速较慢或其他原因 请求等待的过程中 添加loading提示
      // 开启loading  当提示成功或失败的时候会自动关闭Loading
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true, // 禁止背景点击
        duration: 0 // 持续展示
      })

      try {
        await editUserProfile({ birthday: currentDate })
        // 关闭弹层
        this.$emit('close')
        // 更新视图
        this.$emit('input', currentDate)
        // 提示成功
        this.$toast.success('更新成功')
      } catch (err) {
        // 提示失败
        this.$toast.fail('修改昵称失败，请重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
